package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.User;
import com.jiawa.wiki.domain.UserExample;
import com.jiawa.wiki.exception.BusinessException;
import com.jiawa.wiki.exception.BusinessExceptionCode;
import com.jiawa.wiki.mapper.UserMapper;
import com.jiawa.wiki.req.UserLoginReq;
import com.jiawa.wiki.req.UserQueryReq;
import com.jiawa.wiki.req.UserRestPasswordReq;
import com.jiawa.wiki.req.UserSaveReq;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.resp.UserLoginResp;
import com.jiawa.wiki.resp.UserQueryResp;
import com.jiawa.wiki.util.CopyUtil;
import com.jiawa.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Li Cheng
 * @Date 2021/5/15 14:08
 * @Version 1.0
 */

@Service
public class UserService {
    //写日志
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);


    //相当于@Autowired
    @Resource
    private UserMapper userMapper;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 列表复制
     *
     * @param req
     * @return
     */
    public PageResp<UserQueryResp> list(UserQueryReq req) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andLoginNameEqualTo(req.getLoginName());
        }

        PageHelper.startPage(req.getPage(), req.getSize());
        List<User> userList = userMapper.selectByExample(userExample);


        PageInfo<User> pageInfo = new PageInfo<>(userList);
        LOG.info("总行数{}", pageInfo.getTotal());
        LOG.info("总页数{}", pageInfo.getPages());
        //List<UserResp> respList = new ArrayList<>();
        //for (User user: userList) {
        //
        //    //对象复制
        //    UserResp userResp = CopyUtil.copy(user, UserResp.class);
        //
        //    respList.add(userResp);
        //}
        List<UserQueryResp> list = CopyUtil.copyList(userList, UserQueryResp.class);

        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);


        return pageResp;

    }

    /**
     * 保存
     *
     * @param req
     */
    public void save(UserSaveReq req) {
        User user = CopyUtil.copy(req, User.class);
        if (ObjectUtils.isEmpty(req.getId())) {

            User userDB = selectByLoginName(req.getLoginName());
            if (ObjectUtils.isEmpty(userDB)){
                //如果是空的则新增
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                //用户名已经存在
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }

        } else {
            //更新
            user.setLoginName(null);
            user.setPassword(null);

            //下面这个方法不会去修改空的变量
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(Long id) {
       userMapper.deleteByPrimaryKey(id);
    }


    /**
     * 检查用户是否存在
     * @param LoginName
     * @return
     */
    public User selectByLoginName(String LoginName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<User> userList = userMapper.selectByExample(userExample);

        if (CollectionUtils.isEmpty(userList)){
            return null;
        }else {
            return userList.get(0);
        }


    }

    /**
     * 修改密码
     *
     * @param req
     */
    public void restPassword(UserRestPasswordReq req) {
        User user = CopyUtil.copy(req, User.class);

            //下面这个方法不会去修改空的变量
        userMapper.updateByPrimaryKeySelective(user);

    }

    /**
     * 登录功能
     *
     * @param req
     */
    public UserLoginResp login(UserLoginReq req) {
        User userDb = selectByLoginName(req.getLoginName());
          if(ObjectUtils.isEmpty(userDb)){
              //用户名不存在
              //后端日志打印需要详细一点
              LOG.info("用户名不存在，{}",req.getLoginName());
              throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);


          }else {
              if (userDb.getPassword().equals(req.getPassword())){
                  //登录成功
                  UserLoginResp userLoginResp = CopyUtil.copy(userDb, UserLoginResp.class);
                  return userLoginResp;

              }else {
                  //密码不对
                  LOG.info("密码不对，输入密码:{},数据库密码:{}",req.getPassword(),userDb.getPassword());
                  throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);

              }
          }

    }

}



























